﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OptikinetiqControls
{
    public class DrawRotateRect
    {
        public PointF tl = new PointF();
        public PointF tr = new PointF();
        public PointF bl = new PointF();
        public PointF br = new PointF();

        public PointF center = new PointF();

        public double rotate;

        public float width;

        private float _widthHalf;
        public float widthHalf {
            set {
                tl.X = center.X - value;
                tr.X = center.X + value;
                bl.X = center.X - value;
                br.X = center.X + value;
                _widthHalf = value;
                width = _widthHalf * 2;
            }
            get
            {
                return _widthHalf;
            }
        }

        public float height;

        private float _heightHalf;
        public float heightHalf {
            set
            {
                tl.Y = center.Y - value;
                tr.Y = center.Y - value;
                bl.Y = center.Y + value;
                br.Y = center.Y + value;
                _heightHalf = value;
                height = _heightHalf * 2;
            }
            get {
                return _heightHalf;
            }

        }

        private Color _selectColor;
        public Color selectColor
        {
            get { return _selectColor; }
        }

        private Color _penColor;
        public Color penColor
        {
            get { return _penColor; }
            set
            {
                _penColor = value;

                int r = 255 - value.R;
                int g = 255 - value.G;
                int b = 255 - value.B;
                _selectColor = Color.FromArgb(r, g, b);
            }
        }

        private float _penSize;

        public float penSize
        {
            get { return _penSize; }
            set { _penSize = value; }
        }

        public bool isSelect;

        public void MoveCenrer(PointF movePoint)
        {
            float rx = movePoint.X - center.X;
            float ry = movePoint.Y - center.Y;

            tl.X += rx;
            tl.Y += ry;
            tr.X += rx;
            tr.Y += ry;
            bl.X += rx;
            bl.Y += ry;
            br.X += rx;
            br.Y += ry;

            center.X += rx;
            center.Y += ry;
        }

        /// <summary>
        /// 获取面积
        /// </summary>
        /// <returns></returns>
        public float GetArea() {
            return (widthHalf * 2) * (heightHalf * 2);
        }

        /// <summary>
        /// 获取所有角点坐标
        /// </summary>
        /// <returns></returns>
        public PointF[] GetCorners() {
            return new PointF[] { tl, tr, br, bl };
        }

        /// <summary>
        /// 旋转并计算新的角点坐标
        /// </summary>
        /// <param name="rotate">角度</param>
        public void Rotate(double rotate) {
            //计算每个角点当前角度
            tl = ShapeTools.RotatePoint(tl, center, rotate);
            tr = ShapeTools.RotatePoint(tr, center, rotate);
            bl = ShapeTools.RotatePoint(bl, center, rotate);
            br = ShapeTools.RotatePoint(br, center, rotate);
        }

        public PointF GetLeftCenter() {
            float cX = tl.X + (bl.X - tl.X) / 2;
            float cY = tl.Y + (bl.Y - tl.Y) / 2;

            return new PointF(cX, cY);
        }

        public PointF GetRightCenter()
        {
            float cX = tr.X + (br.X - tr.X) / 2;
            float cY = tr.Y + (br.Y - tr.Y) / 2;

            return new PointF(cX, cY);
        }

        public PointF GetBottomCenter() {
            float cX = bl.X + (br.X - bl.X) / 2;
            float cY = bl.Y + (br.Y - bl.Y) / 2;

            return new PointF(cX, cY);
        }

        public float GetCenterSize() {
            return Math.Min(width, height) / 10;
        }

    }
}
